package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.StoreService;

/**
 * Servlet implementation class StoreDeal
 */
@WebServlet("/files/StoreDeal")
public class StoreDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int LM_NAME = 60;
	private static final int LM_ROOM = 20;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StoreDeal() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		switch (request.getParameter("deal")) {
			case "add": {
				String name = request.getParameter("name");
				String room = request.getParameter("room");
				if (name == null || room == null || name.length() > LM_NAME
						|| room.length() > LM_ROOM)
					break;
				StoreService.storeAdd(name, room);
				break;
			}
			case "edit": {
				String name = request.getParameter("name");
				String room = request.getParameter("room");
				if (name == null || room == null || name.length() > LM_NAME
						|| room.length() > LM_ROOM)
					break;
				StoreService.storeEdit(name, room);
				break;
			}
			case "delete": {
				if( StoreService.storeDelete(request) )
					break;
				else{
					response.sendRedirect("storeErrorDel.jsp");
					return;
				}
			}
			default: {
				return;
			}
		}
		response.sendRedirect("WP_Storage.jsp");

	}

}
